/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.braveo.dict;

import java.io.IOException;
import java.io.StringReader;

/**
 *
 * @author Yongjie
 */
public class AndroidWord {
    private String word;
    private String explain;

    public AndroidWord(String word, String explain) {
        this.word = word;
        this.explain = explain;
    }

    public String getExplain() {
        return explain;
    }

    public String getWord() {
        return word;
    }

    public void setExplain(String explain) {
        this.explain = explain;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getWordAsSqlVariable() {
        StringBuilder sb = new StringBuilder();
        StringReader sr = new StringReader(word);
        char c;

        try{
            int x;
            while((x = sr.read())>0){
                c = (char)x;
                if(c != '\''){
                    sb.append(c);
                }else{
                    sb.append("''");
                }
            }
        }catch(IOException ioe){

        }finally{
            sr.close();
        }

        //System.out.println("");
        return sb.toString();
    }

    public String getExplainAsSqlVariable() {
        StringBuilder sb = new StringBuilder();
        StringReader sr = new StringReader(explain);
        char c;

        try{
            int x;
            boolean escape = false;
            while((x = sr.read())>0){
                c = (char)x;
                if(c == '\\'){
                    if(escape)
                        sb.append('\\');
                    escape = !escape;
                }else if(escape){
                    switch(c){
                        case 'n':
                            sb.append('\n');
                            break;
                        default:
                            sb.append('\\').append(c);
                            //System.out.println("WARNING: (escape) " + explain);
                    }
                    escape = false;
                }else if(c != '\''){
                    sb.append(c);
                }else{
                    sb.append("''");
                }
            }
        }catch(IOException ioe){

        }finally{
            sr.close();
        }

        return sb.toString();
    }


}
